package org.linlinjava.litemall.wx.web;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCollect;
import org.linlinjava.litemall.db.domain.LitemallCommodity;
import org.linlinjava.litemall.db.service.LitemallCollectService;
import org.linlinjava.litemall.db.service.LitemallCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户收藏服务
 */
@RestController
@RequestMapping("/wx/collect")
@Validated
public class WxCollectController {
	private final Log logger = LogFactory.getLog(WxCollectController.class);

	@Autowired
	private LitemallCollectService collectService;
	@Autowired
	private LitemallCommodityService commodityService;

	/**
	 * 用户收藏列表
	 * @return 用户收藏列表
	 */
	@PostMapping("list")
	public Object list(@RequestBody String json, HttpServletRequest request) {
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		Integer userId = Integer.parseInt(user.getUserid());
		Byte type = JacksonUtil.parseByte(json, "type");
		Integer page = JacksonUtil.parseInteger(json, "page");
		Integer limit = JacksonUtil.parseInteger(json, "limit");
		String sort = JacksonUtil.parseString(json, "sort");
		String order = JacksonUtil.parseString(json, "order");
		if (StringUtils.isEmpty(sort)) {
			sort = "add_time";
		}
		if (StringUtils.isEmpty(order)) {
			sort = "desc";
		}

		List<LitemallCollect> collectList = collectService.queryByType(userId, type, page, limit, sort, order);
		List<Object> collects = new ArrayList<>(collectList.size());
		for (LitemallCollect collect : collectList) {
			Map<String, Object> c = new HashMap<>();
			c.put("id", collect.getId());
			c.put("type", collect.getType());
			c.put("valueId", collect.getValueId());

			LitemallCommodity goods = commodityService.selectById(collect.getValueId());
			c.put("name", goods.getCommodityName());
			c.put("brief", goods.getCommodityDescription());
			c.put("picUrl", goods.getCommodityMainImage());
			c.put("retailPrice", goods.getCommodityPrice());
			collects.add(c);
		}
		return ResponseUtil.okList(collects, collectList);
	}

	/**
	 * 用户收藏添加或删除
	 * <p>
	 * 如果商品没有收藏，则添加收藏；如果商品已经收藏，则删除收藏状态。
	 * @param body   请求内容，{ type: xxx, valueId: xxx }
	 * @return 操作结果
	 */
	@PostMapping("addordelete")
	public Object addordelete(@RequestBody String body,HttpServletRequest request) {
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		Integer userId = Integer.parseInt(user.getUserid());
		Byte type = JacksonUtil.parseByte(body, "type");
		Integer valueId = JacksonUtil.parseInteger(body, "valueId");
		if (!ObjectUtils.allNotNull(type, valueId)) {
			return ResponseUtil.badArgument();
		}
		LitemallCollect collect = collectService.queryByTypeAndValue(userId, type, valueId);
		if (collect != null) {
			collectService.deleteById(collect.getId());
		} else {
			collect = new LitemallCollect();
			collect.setUserId(userId);
			collect.setValueId(valueId);
			collect.setType(type);
			collectService.add(collect);
		}
		return ResponseUtil.ok();
	}
	//添加收藏
	@PostMapping("insert")
	public Object insert(@RequestBody String body,HttpServletRequest request){
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		Integer userId = Integer.parseInt(user.getUserid());
		//Integer userId = JacksonUtil.parseInteger(body, "userId");
		Byte type = JacksonUtil.parseByte(body, "type");
		Integer valueId = JacksonUtil.parseInteger(body, "valueId");
		if (!ObjectUtils.allNotNull(type, valueId)) {
			return ResponseUtil.badArgument();
		}
		LitemallCollect collect = collectService.queryByTypeAndValue(userId, type, valueId);
		if (collect != null) {
			return ResponseUtil.ok("已有收藏");
		} else {
			collect = new LitemallCollect();
			collect.setUserId(userId);
			collect.setValueId(valueId);
			collect.setType(type);
			collectService.add(collect);
		}
		return ResponseUtil.ok("收藏成功");
	}
	//删除收藏
	@GetMapping("delete")
	public Object delete(Integer id){
		collectService.deleteById(id);
		return ResponseUtil.ok();
	}
}